package cate.game.play.buff.h;

import cate.common.util.XT;
import cate.game.play.buff.BuffHandler;
import cate.game.play.buff.BuffParam;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.fighter.Fighter;
import cate.game.play.proce.action.ctx.ActionCtx;
import easy.java.struct.ref.DoubleRef;

import java.util.List;

/**
 * 受到致命伤害后的该回合免疫所有致死伤害，只能触发1次，持续3回合，
 * 结束时将到的40%伤害转化为护盾值平摊给队友，持续2回合
 */
public class 笔走游龙BH extends BuffHandler {

	private double 伤害系数;

	private int 护盾;

	//伤害系数=4000&护盾=？？
	@Override
	public void parse(String code) {
		PlayCfgArgs args = new PlayCfgArgs(code);
		伤害系数 = args.getWDouble("伤害系数", 0d);
		护盾 = args.getInt("护盾", 0);
	}

	private boolean ignDeadDamage;

	@Override
	public void adjustDeadlyDamage(ActionCtx action, Fighter actor, int reason, DoubleRef doubleRef) {
		ignDeadDamage = true;
		doubleRef.v = 0;
	}

	@Override
	public void onRoundEnd(ActionCtx action) {
		if (ignDeadDamage) {
			buff.markInvalid(action);
		}
	}

	@Override
	public void onInvalid(ActionCtx action, byte invalidReason) {
		long hurt = buff.owner.performance.calTotalHurt(buff.roundAdd, action.getRound().index);
		if (hurt > 0) {
			List<Fighter> friends = buff.owner.fightTeam.getFriends(buff.owner);
			if (XT.isNotBlank(friends)) {
				double value = hurt * 伤害系数 / friends.size();
				BuffParam param = new BuffParam().buffHpSet(value);
				for (Fighter friend : friends) {
					friend.buff.tryAddByTid(action, buff.owner, 护盾, param);
				}
			}
		}
	}
}
